﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using System.Reflection;
using System.Collections.Specialized;
using System.Globalization;
using System.Xml;
using System.Text.RegularExpressions;

namespace SF.Base.Core {
	public class BaseSettings {
		private static BaseSettings cacheSingleton;

		/// <summary>
		/// 对象 防并发
		/// </summary>
		private static object syncLock = new object();

		/// <summary>
		/// 单体缓存
		/// </summary>
		public static BaseSettings Instance {
			get {
				if (cacheSingleton == null) {
					lock (syncLock) {
						if (cacheSingleton == null) {
							cacheSingleton = new BaseSettings();
						}
					}
				}
				return cacheSingleton;
			}
		}

		public static void Clear() {
			cacheSingleton = null;
		}

		/// <summary>
		/// 私有构造函数
		/// </summary>
		private BaseSettings() {
			ConnSF = ConfigurationManager.ConnectionStrings["SFConnectionString"].ConnectionString;

			Type settingsType = this.GetType();
			//web.config配置
			NameValueCollection appSettings = ConfigurationManager.AppSettings;

			//数据库配置
			List<SettingEntity> sqlSettings = SettingRules.GetSettings(ConnSF);

			Logger.Detail("appSettings", appSettings.Count);

			Logger.Detail("settingsType", settingsType.GetProperties().Length);

			foreach (PropertyInfo propertyInformation in settingsType.GetProperties()) {
				Logger.Detail("propertyInformation.Name", propertyInformation.Name);
				try {
					if (propertyInformation.CanWrite) {
						if (sqlSettings != null && sqlSettings.Count > 0) {
							//将数据库中的配置赋值给对象属性
							foreach (SettingEntity setting in sqlSettings) {
								if (setting.Status == 1 && propertyInformation.Name.Equals(setting.Key, StringComparison.OrdinalIgnoreCase)) {
									Logger.Detail("dt_settings", setting.Key, setting.Value);
									propertyInformation.SetValue(this, Convert.ChangeType(setting.Value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
									break;
								}
							}
						}

						if (appSettings != null && appSettings.AllKeys.Length > 0) {
							foreach (string key in ConfigurationManager.AppSettings) {
								//将Web.Config中的配置赋值给对象属性
								if (propertyInformation.Name.Equals(key, StringComparison.OrdinalIgnoreCase)) {
									Logger.Detail("Appsettings", key, appSettings[key]);
									propertyInformation.SetValue(this, Convert.ChangeType(appSettings[key], propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
									break;
								}
							}
						}
					}
				}
				catch (Exception ex) {
					Logger.Exception(ex);
				}
			}
		}

		/// <summary>
		/// SF数据库连接字符串
		/// </summary>
		public string ConnSF {
			get;
			set;
		}

		/// <summary>
		/// Master数据库连接字符串
		/// </summary>
		public string ConnMaster {
			get;
			set;
		}

		/// <summary>
		/// 是否打开网站评论
		/// </summary>
		public Boolean IsOpenReview {
			get;
			set;
		}

		/// <summary>
		/// taobao开发平台app key
		/// </summary>
		public string TaobaoAppkey {
			get;
			set;
		}
		/// <summary>
		/// taobao开发平台App Secret
		/// </summary>
		public string TaobaoAppsecret {
			get;
			set;
		}

		/// <summary>
		/// Taobao开发平台地址
		/// 正式环境 http://gw.api.taobao.com/router/rest
		/// 测试环境 http://gw.api.tbsandbox.com/router/rest
		/// </summary>
		public string TaobaoApiPath {
			get;
			set;
		}

		/// <summary>
		/// 淘宝客账号集合
		/// </summary>
		public string TaoBaoKeAccounts {
			get;
			set;
		}

		/// <summary>
		/// 随机取一个淘宝客账号
		/// </summary>
		public string TaoBaoKeAccount {
			get {
				if (string.IsNullOrEmpty(TaoBaoKeAccounts)) {
					throw new Exception("淘宝客账号未配置");
				}
				string[] arr = Regex.Split(TaoBaoKeAccounts, ",");
				if (arr.Length == 1) {
					return arr[0];
				}

				Random rd = new Random((int)(DateTime.Now.Ticks));
				return arr[rd.Next(arr.Length)];
			}
		}

		/// <summary>
		/// 淘宝客账号的URL标识
		/// </summary>
		public string TaoBaoKeKeys {
			get;
			set;
		}

		/// <summary>
		/// 随机取一个淘宝客账号的URL标识
		/// </summary>
		public string TaoBaoKeKey {
			get {
				if (string.IsNullOrEmpty(TaoBaoKeKeys)) {
					throw new Exception("淘宝客账号的URL标识未配置");
				}
				string[] arr = Regex.Split(TaoBaoKeKeys, ",");
				if (arr.Length == 1) {
					return arr[0];
				}

				Random rd = new Random((int)(DateTime.Now.Ticks));
				return arr[rd.Next(arr.Length)];
			}
		}
	}
}
